/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.scm.hibernate.entities;

import com.scm.hibernate.utilities.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Krishna
 */
@Table(name = "divisions")
@Entity
public class Division extends AbstractEntity{

    @GeneratedValue

    @Id
    @Column(name = "id")
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "bid")
    private Long bid;
    
    @Transient
    private Branch br;

    @Column(name = "semester")
    private int semester;

    public Branch getBr() {
        return br;
    }

    public void setBr(Long bid) {
        this.br = (Branch)new Branch().getById(bid);
    }

    
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getBid() {
        return bid;
    }

    public void setBid(Long bid) {
        this.bid = bid;
    }

    public int getSemester() {
        return semester;
    }

    public void setSemester(int semester) {
        this.semester = semester;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public List<Division> getAllByBranchid() {
        List<Division> list = null;
        // ArrayList<Long> list_id=new ArrayList<>();
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();

            Query query = session.createQuery("from Division where bid= '" + this.bid + "' ");
            list = query.list();

//            for(Division d:list)
//            {
//                long flag=d.getId();
//                
//                list_id.add(flag);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<Long> myfunction(String str) {

//         Session s=HibernateUtil.getSessionFactory().openSession();
//        Criteria  criteria  =s.createCriteria(Division.class);
//       criteria.setProjection(Projections.property("id")); //Projections.property is used to retrieve specific columns
        List<Long> li;
        Session session = HibernateUtil.getSessionFactory().openSession();
        String select = "FROM Divison d  inner join Branch b where d.bid=b.id AND b.name=" + str + " ";
        Query query = session.createQuery(select);

        li = query.list();

        return li;
    }
    
    public  List<Division> getDivisions(Long branch_id,Long semester)
    {
        List<Division> li= null;
        
        try 
        {
            Session s=HibernateUtil.getSessionFactory().openSession();
            
            Query q=s.createQuery("from Division where bid="+branch_id+" AND semester="+semester+"");
            li=q.list();
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return li;
    }

}
